<template>
  <div class="app-container">
    <el-tabs v-model="tabKey" class="tabs-top" @tab-change="handleClick">
      <el-tab-pane label="还款录入" name="1"></el-tab-pane>
      <el-tab-pane label="还款确认" name="2"></el-tab-pane>
    </el-tabs>
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :xs="24">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
          <el-form-item label="" prop="id">
            <el-input v-model="queryParams.id" placeholder="担保编号" clearable style="width: 200px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item prop="user_name_tel">
            <el-input v-model="queryParams.user_name_tel" placeholder="姓名/手机号" clearable style="width: 200px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item prop="contract_code">
            <el-input v-model="queryParams.contract_code" placeholder="合同号" clearable style="width: 200px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item prop="loan_type">
            <el-select v-model="queryParams.loan_type" placeholder="担保类型" clearable style="width: 200px">
              <el-option v-for="dict in loanType" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item style="width: 300px" prop="dateRange">
            <el-date-picker
              v-model="queryParams.dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="dateChange"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            <!-- <right-toolbar v-if="tabKey == 1" @queryTable="getList" :columns="columns"></right-toolbar> -->
            <!-- <right-toolbar @queryTable="getList" :columns="columnst"></right-toolbar> -->
          </el-form-item>
        </el-form>
        <!-- <el-row :gutter="10" class="mb8 function-zone"> -->
          <!-- <el-col :span="1.5">
                  <el-button
                     type="info"
                     plain
                     icon="Upload"
                     @click="handleImport"
                     v-hasPermi="['system:user:import']"
                  >导入</el-button>
               </el-col> -->
          <!-- <el-col :span="1.5">
                  <el-button
                     type="warning"
                     plain
                     icon="Upload"
                     @click="handleExport"
                     v-hasPermi="['system:user:export']"
                  >导出</el-button>
               </el-col> -->
        <!-- </el-row> -->
        <el-table v-if="tabKey == 1" v-loading="loading" :data="loanList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="担保编号" align="center" key="id" prop="id" v-if="columns[0].visible" />
          <el-table-column label="姓名" align="center" key="user_name" prop="user_name" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="合同号" align="center" key="contract_code" prop="contract_code" v-if="columns[2].visible" />
          <el-table-column label="申请日期" align="center" key="create_time" prop="create_time" v-if="columns[3].visible" />
          <el-table-column label="申请额度" align="center" key="apply_quota" prop="apply_quota" v-if="columns[4].visible" />
          <el-table-column label="审批金额" align="center" key="approval_amount" prop="approval_amount" v-if="columns[5].visible" />
          <el-table-column label="合同金额" align="center" key="contract_amount" prop="contract_amount" v-if="columns[6].visible" />
          <el-table-column label="担保期限" align="center" key="loan_stages" prop="loan_stages" v-if="columns[7].visible" width="100" />
          <el-table-column label="担保类型" align="center" key="loan_type_name" prop="loan_type_name" v-if="columns[8].visible" />
          <el-table-column label="还款方式" align="center" key="method_type" prop="method_type" v-if="columns[9].visible" />
          <el-table-column label="放款时间" align="center" key="loan_time" prop="loan_time" v-if="columns[10].visible" />
          <el-table-column label="放款通道" align="center" key="channel_type" prop="channel_type" v-if="columns[11].visible" />
          <el-table-column label="状态" align="center" key="status_type" prop="status_type" v-if="columns[12].visible" />
          <el-table-column label="操作" fixed="right" align="center" width="150" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-tooltip content="查看" placement="top">
                <el-button link type="primary" icon="View" @click="handleView(scope.row, true)" v-hasPermi="['system:user:edit']"></el-button>
              </el-tooltip>
              <el-tooltip content="还款录入" placement="top">
                <el-button link type="primary" icon="EditPen" @click="handleView(scope.row, false)" v-hasPermi="['system:user:remove']"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <el-table v-else v-loading="loading" :data="loanList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="担保编号" align="center" key="id" prop="id" v-if="columns[0].visible" />
          <el-table-column label="姓名" align="center" key="user_name" prop="user_name" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="合同号" align="center" key="contract_code" prop="contract_code" v-if="columns[2].visible" />
          <el-table-column label="申请日期" align="center" key="create_time" prop="create_time" v-if="columns[3].visible" />
          <el-table-column label="申请额度" align="center" key="apply_quota" prop="apply_quota" v-if="columns[4].visible" />
          <el-table-column label="审批金额" align="center" key="approval_amount" prop="approval_amount" v-if="columns[5].visible" />
          <el-table-column label="合同金额" align="center" key="contract_amount" prop="contract_amount" v-if="columns[6].visible" />
          <el-table-column label="担保期限" align="center" key="loan_stages" prop="loan_stages" v-if="columns[7].visible" width="100" />
          <el-table-column label="担保类型" align="center" key="loan_type_name" prop="loan_type_name" v-if="columns[8].visible" />
          <el-table-column label="还款方式" align="center" key="method_type" prop="method_type" v-if="columns[9].visible" />
          <el-table-column label="是否放款" align="center" key="isLoan" prop="isLoan" v-if="columnst[10].visible" />
          <el-table-column label="放款时间" align="center" key="loanDate" prop="loanDate" v-if="columnst[11].visible" />
          <el-table-column label="放款通道" align="center" key="loanWay" prop="loanWay" v-if="columnst[12].visible" />
          <el-table-column label="状态" align="center" key="status_type" prop="status_type" v-if="columnst[13].visible" />
          <el-table-column label="操作" fixed="right" align="center" width="150" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-tooltip content="查看" placement="top">
                <el-button link type="primary" icon="View" @click="handleView(scope.row, true)" v-hasPermi="['system:user:edit']"></el-button>
              </el-tooltip>
              <el-tooltip content="还款确认" placement="top">
                <el-button link type="primary" icon="FolderChecked" @click="handleView(scope.row, false)" v-hasPermi="['system:user:remove']"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page_size" v-model:limit="queryParams.page_count" @pagination="getList" />
      </el-col>
    </el-row>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
      <el-upload
        ref="uploadRef"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <template #tip>
          <div class="el-upload__tip text-center">
            <div class="el-upload__tip"><el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据</div>
            <span>仅允许导入xls、xlsx格式文件。</span>
            <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>
          </div>
        </template>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Pay">
import { methodType, statusType, channelType, loanTypes } from "@/utils/dict";
import { getToken } from "@/utils/auth";
import { loanRepaymentList } from "@/api/afterLoan/index";
import { ElLoading } from "element-plus";
import { reactive } from "vue";
const router = useRouter();
const { proxy } = getCurrentInstance();
// const { sys_normal_disable, sys_user_sex } = proxy.useDict("sys_normal_disable", "sys_user_sex");
const loanList = ref([]);
const tabKey = ref("1");
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const loanType = ref([
  {
    value: "1",
    label: "个人信用",
  },
  {
    value: "2",
    label: "个人分期",
  },
  {
    value: "3",
    label: "个人抵押",
  },
  {
    value: "4",
    label: "企业信用",
  },
  {
    value: "5",
    label: "企业分期",
  },
  {
    value: "6",
    label: "企业租赁",
  },
]);
/*** 用户导入参数 */
const upload = reactive({
  // 是否显示弹出层（用户导入）
  open: false,
  // 弹出层标题（用户导入）
  title: "",
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/system/user/importData",
});
// 列显隐信息
const columns = ref([
  { key: 0, label: `担保编号`, visible: true },
  { key: 1, label: `姓名`, visible: true },
  { key: 2, label: `合同号`, visible: true },
  { key: 3, label: `申请日期`, visible: true },
  { key: 4, label: `申请额度`, visible: true },
  { key: 5, label: `审批金额`, visible: true },
  { key: 6, label: `合同金额`, visible: true },
  { key: 7, label: `担保期限`, visible: true },
  { key: 8, label: `担保类型`, visible: true },
  { key: 9, label: `还款方式`, visible: true },
  { key: 10, label: `放款时间`, visible: true },
  { key: 11, label: `放款通道`, visible: true },
  { key: 12, label: `状态`, visible: true },
]);
// 列显隐信息
const columnst = ref([
  { key: 0, label: `担保编号`, visible: true },
  { key: 1, label: `姓名`, visible: true },
  { key: 2, label: `合同号`, visible: true },
  { key: 3, label: `申请日期`, visible: true },
  { key: 4, label: `申请额度`, visible: true },
  { key: 5, label: `审批金额`, visible: true },
  { key: 6, label: `合同金额`, visible: true },
  { key: 7, label: `担保期限`, visible: true },
  { key: 8, label: `担保类型`, visible: true },
  { key: 9, label: `还款方式`, visible: true },
  { key: 10, label: `是否放款`, visible: true },
  { key: 11, label: `放款时间`, visible: true },
  { key: 12, label: `放款通道`, visible: true },
  { key: 13, label: `状态`, visible: true },
]);
const data = reactive({
  queryParams: {
    page_size: 1,
    page_count: 10,
    id: undefined,
    contract_code: undefined,
    user_name_tel: undefined,
    user_name: undefined,
    user_phone: undefined,
    loan_type: undefined,
    start_time: undefined,
    end_time: undefined,
    dateRange: undefined,
  },
});
const { queryParams } = toRefs(data);
/**切换导航*/
function handleClick(key) {
  tabKey.value = key;
  resetQuery();
}
/** 还款查看 */
function handleView(row, vl) {
  router.push({
    name: "Paydetail",
    state: {
      infos: {
        type: vl,
        status: tabKey.value,
        id: row.id,
        purpose_name: row.purpose_name,
        debit_card_id: row.debit_card_id,
        user_name: row.user_name,
        approval_rate: row.approval_rate,
        loan_type_classify: row.loan_type_name.slice(0, 2),
        loan_type_name: row.loan_type_name.slice(2, 5),
        loan_stages: row.loan_stages + "个月",
        apply_quota: !row.apply_quota ? row.apply_quota : row.apply_quota + "元",
        method_type: row.method_type,
        contract_amount: !row.contract_amount ? row.contract_amount : row.contract_amount + "元",
        approval_amount: !row.approval_amount ? row.approval_amount : row.approval_amount + "元",
      },
    },
  });
}
function dateChange(val) {
  queryParams.value.start_time = queryParams.value.dateRange[0] + " 00:00:00";
  queryParams.value.end_time = queryParams.value.dateRange[1] + " 23:59:59";
}
/** 查询用户列表 */
function getList() {
  loading.value = true;
  queryParams.value.user_phone = queryParams.value.user_name_tel;
  queryParams.value.user_name = queryParams.value.user_name_tel;
  loanRepaymentList({
    ...queryParams.value,
    status: tabKey.value,
  }).then((res) => {
    loading.value = false;
    total.value = res.data.total_page;
    if (res.data.total_page > 0) {
      loanList.value = res.data.list.map((item) => {
        item["channel_type"] = channelType(item.channel);
        item["status_type"] = statusType(item.status);
        item["method_type"] = methodType(item.method);
        item["isLoan"] = statusType(item.status);
        item["loan_type_name"] = loanTypes(item.loan_type);
        return item;
      });
    } else {
      loanList.value = [];
    }
  });
}
/**选中条数*/
function handleSelectionChange() {}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page_size = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.start_time = "";
  queryParams.value.end_time = "";
  handleQuery();
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download("loan/export/loanRepaymentList", {
    ...queryParams.value,
    status: tabKey.value,
  });
}
/** 导入按钮操作 */
function handleImport() {
  upload.title = "用户导入";
  upload.open = true;
}
/** 下载模板操作 */
function importTemplate() {
  proxy.download("system/user/importTemplate", {}, `user_template_${new Date().getTime()}.xlsx`);
}
/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
  upload.isUploading = true;
};
/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
  upload.open = false;
  upload.isUploading = false;
  proxy.$refs["uploadRef"].handleRemove(file);
  proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
  getList();
};
/** 提交上传文件 */
function submitFileForm() {
  proxy.$refs["uploadRef"].submit();
}
getList();
onActivated(() => {
  let timer = setTimeout(() => {
    getList();
    clearTimeout(timer);
  }, 1100);
});
</script>
<style scoped>
.tabs-top {
  margin-bottom: 5px;
}
.function-zone {
  margin-bottom: 20px;
}
.explain {
  line-height: 40px;
  font-size: 12px;
  margin-left: 90px;
}
</style>
